package com.csdj.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * packageName com.csdj.web.filter
 *
 * @author winver
 * @version 1.0
 * @className IPFilter
 * @date 2024/11/28
 * @description TODO
 */
@WebFilter("/*")
public class IPFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
         //获取IP地睛
        String ip = servletRequest.getRemoteAddr();
        System.out.println(ip);
        List<String> ips = Arrays.asList("");
        if(!ips.contains(ip)){
            //继续放行
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
           HttpServletResponse resp = (HttpServletResponse) servletResponse;
           resp.setContentType("text/html;charset=utf-8");
           resp.getWriter().write("谢谢你的访问，但你的IP被我限制了");
        }

    }
}
